/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.alipay.util;

import com.alipay.api.domain.Article;
import com.alipay.constants.AlipayServiceEnvConstants;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 消息构造工具
 *
 * @author baoxing.gbx
 * @version $Id: AlipayMsgBuildUtil.java, v 0.1 Jul 24, 2014 5:47:19 PM baoxing.gbx Exp $
 */
public class AlipayMsgBuildUtil {

    /**
     * 构造单发图文消息
     *
     * @param fromUserId
     * @return
     */
    public static String buildSingleImgTextMsg(String fromUserId) {

        StringBuilder sb = new StringBuilder();

        //构建json格式单发图文消息: 所有内容开发者请根据自有业务自行设置响应值，这里只是个样例
        sb.append("{'articles':[{'action_name':'立即查看','desc':'这是图文内容','image_url':'http://pic.alipayobjects.com/e/201311/1PaQ27Go6H_src.jpg','title':'这是标题','url':'https://www.com.alipay.com/'}],'msg_type':'image-text','to_user_id':'"
                + fromUserId + "'}");

        return sb.toString();
    }

    /**
     * 构造群发图文消息
     *
     * @return
     */
    public static String buildGroupImgTextMsg() {

        StringBuilder sb = new StringBuilder();

        //构建json格式群发图文消息: 所有内容开发者请根据自有业务自行设置响应值，这里只是个样例
        sb.append("{'articles':[{'action_name':'立即查看','desc':'这是图文内容','image_url':'http://pic.alipayobjects.com/e/201311/1PaQ27Go6H_src.jpg','title':'这是标题','url':'https://www.com.alipay.com/'}],'msg_type':'image-text'}");

        return sb.toString();
    }



    /**
     * 构造网吧列表链接
     * @param fromUserId
     * @param fromAlipayUserId
     * @return
     */
    public static String buildSingleImgTextMsgTOWangbaList(String fromUserId,String fromAlipayUserId) {

        //从Spring中获取bean
//        AlipayUserService alipayUserService= SpringContextHolder.getBean(AlipayUserService.class);
//        //获取用户
//        Integer aId =alipayUserService.getIdByAlipayId(fromAlipayUserId);
//        if(aId==null){
//            return buildSingleTextMsg(fromUserId,"未获取到用户信息，请重新关注服务窗");
//        }

        Article article = new Article();
        article.setTitle("网费充值");
        article.setActionName("点我充值");
        article.setImageUrl("http://pic.alipayobjects.com/e/201311/1PaQ27Go6H_src.jpg");
        article.setUrl("http://a.taofairy.com/#/show/wangbaslist".replace("/#", "/?" + System.currentTimeMillis() + "/#") + "/" + 23 + "?_timestrap=" + System.currentTimeMillis()); //调到长老的页面
        //article.setUrl(toWangbaListGateway + "?aId=" + aId);

        return buildSingleImgTextMsg(fromUserId, article);
    }
    /**
     * 构造单图文消息
     * @param fromUserId            要发送的用户id
     * @param article               消息体
     * @return
     */
    private static String buildSingleImgTextMsg(String fromUserId,Article article){
        JSONObject msg = new JSONObject();
        List<Article> articles = new ArrayList<Article>();
        articles.add(article);
        msg.put("articles", articles);
        msg.put("msgType", "image-text");
        msg.put("toUserId", fromUserId);
        LogUtil.log("buildSingleImgTextMsg", msg.toString());
        return msg.toString();
    }


    /**
     * 构造单发纯文本消息
     *
     * @param fromUserId
     * @return
     */
    public static String buildSingleTextMsg(String fromUserId) {

        StringBuilder sb = new StringBuilder();

        //构建json格式单发纯文本消息体： 所有内容开发者请根据自有业务自行设置响应值，这里只是个样例
        sb.append("{'msg_type':'text','text':{'content':'这是纯文本消息'}, 'to_user_id':'" + fromUserId
                + "'}");

        return sb.toString();
    }

    /**
     * 构造群发纯文本消息
     *
     * @return
     */
    public static String buildGroupTextMsg() {

        StringBuilder sb = new StringBuilder();

        //构建json格式群发纯文本消息体： 所有内容开发者请根据自有业务自行设置响应值，这里只是个样例
        sb.append("{'msg_type':'text','text':{'content':'这是纯文本消息'}}");

        return sb.toString();
    }

    /**
     * 构造免登图文消息
     *
     * @param fromUserId
     * @return
     */
    public static String buildImgTextLoginAuthMsg(String fromUserId) {

        StringBuilder sb = new StringBuilder();

        //免登连接地址，开发者需根据部署服务修改相应服务ip地址
        String url = "http://10.15.132.68:8080/AlipayFuwuDemo/loginAuth.html";

        //构建json格式的单发免登图文消息体     authType 等于 "loginAuth"表示免登消息 ： 所有内容开发者请根据自有业务自行设置响应值，这里只是个样例
        sb.append("{'articles':[{'action_name':'立即查看','desc':'这是图文内容','image_url':'http://pic.alipayobjects.com/e/201311/1PaQ27Go6H_src.jpg','title':'这是标题','url':'"
                + url
                + "', 'auth_type':'loginAuth'}],'msg_type':'image-text', 'to_user_id':'"
                + fromUserId + "'}");

        return sb.toString();
    }

    /**
     * 构造基础的响应消息
     *
     * @return
     */
    public static String buildBaseAckMsg(String fromUserId) {
        StringBuilder sb = new StringBuilder();
        sb.append("<XML>");
        sb.append("<ToUserId><![CDATA[" + fromUserId + "]]></ToUserId>");
        sb.append("<AppId><![CDATA[" + AlipayServiceEnvConstants.APP_ID + "]]></AppId>");
        sb.append("<CreateTime>" + Calendar.getInstance().getTimeInMillis() + "</CreateTime>");
        sb.append("<MsgType><![CDATA[ack]]></MsgType>");
        sb.append("</XML>");
        return sb.toString();
    }

}
